<template>
	<a-drawer
		:title="formData.id ? '编辑柜门权限' : '增加柜门权限'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="用户ID：" name="userId">
						<a-input v-model:value.trim="formData.userId" placeholder="请输入用户ID" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="授权类型：" name="deviceType">
						<a-select v-model:value="formData.deviceType" placeholder="请选择授权类型" :options="deviceTypeOptions" />
					</a-form-item>
				</a-col>
			</a-row>

			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="用户申请天数：" name="userApplyDays">
						<a-input-number
							v-model:value="formData.userApplyDays"
							placeholder="请输入用户申请天数"
							addon-after="天"
							:precision="0"
							:min="0"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="授权天数：" name="permDays">
						<a-input-number
							v-model:value="formData.permDays"
							placeholder="请输入授权天数"
							addon-after="天"
							:precision="0"
							:min="0"
							allow-clear
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="授权开始时间：" name="permStartTime">
						<a-date-picker
							v-model:value="formData.permStartTime"
							value-format="YYYY-MM-DD HH:mm:ss"
							show-time
							placeholder="请选择授权开始时间"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="授权结束时间：" name="permEndTime">
						<a-date-picker
							v-model:value="formData.permEndTime"
							value-format="YYYY-MM-DD HH:mm:ss"
							show-time
							placeholder="请选择授权结束时间"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="冻结开始时间：" name="frozenStartTime">
						<a-date-picker
							v-model:value="formData.frozenStartTime"
							value-format="YYYY-MM-DD HH:mm:ss"
							show-time
							placeholder="请选择冻结开始时间"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="冻结结束时间：" name="frozenEndTime">
						<a-date-picker
							v-model:value="formData.frozenEndTime"
							value-format="YYYY-MM-DD HH:mm:ss"
							show-time
							placeholder="请选择冻结结束时间"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-form-item label="授权状态：" name="permState">
				<a-select v-model:value="formData.permState" placeholder="请选择授权状态" :options="permStateOptions" />
			</a-form-item>
			<a-form-item label="组柜ID：" name="cabinetGroupId">
				<a-input v-model:value.trim="formData.cabinetGroupId" placeholder="请输入组柜ID" allow-clear />
			</a-form-item>
			<a-form-item label="柜子ID：" name="cabinetInfoId">
				<a-input v-model:value.trim="formData.cabinetInfoId" placeholder="请输入柜子ID" allow-clear />
			</a-form-item>
			<a-form-item label="柜门ID：" name="cabinetDoorId">
				<a-input v-model:value.trim="formData.cabinetDoorId" placeholder="请输入柜门ID" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="bizCabinetPermissionForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCabinetPermissionReagentApi from '@/api/biz/bizCabinetPermissionReagentApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const permStateOptions = ref([])
	const deviceTypeOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		permStateOptions.value = [
			{
				label: '待授权',
				value: 'UNAUTHORIZED'
			},
			{
				label: '已拒绝',
				value: 'REFUSED'
			},
			{
				label: '已授权',
				value: 'AUTHORIZED'
			},
			{
				label: '已冻结',
				value: 'FROZEN'
			},
			{
				label: '已过期',
				value: 'EXPIRED'
			}
		]
		deviceTypeOptions.value = [
			{
				label: '组柜',
				value: 'CABINET_GROUP'
			},
			{
				label: '柜子',
				value: 'CABINET_INFO'
			},
			{
				label: '柜门',
				value: 'CABINET_DOOR'
			}
		]
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizCabinetPermissionReagentApi
				.bizCabinetPermissionSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
